package licode.unisop.single.user.infrastructure.repository.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import licode.unisop.provider.vo.SiPageResult;
import licode.unisop.realm.client.dto.SiRealmDTO;
import licode.unisop.realm.client.info.SiRealmQueryPageIn;
import licode.unisop.single.user.domain.aggregate.entity.RealmsEntity;
import licode.unisop.single.user.domain.aggregate.repository.RealmsDao;
import licode.unisop.single.user.infrastructure.assembler.UniUserSingleAssembler;
import licode.unisop.single.user.infrastructure.repository.mapper.RealmsMapper;
import licode.unisop.single.user.infrastructure.repository.po.RealmsPO;
import licode.unisop.single.user.infrastructure.utils.PageUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * uni-user-single
 *
 * @author licode
 * @date 2016-12-10 09:34:29
 */
@Component
public class RealmsDaoImpl
        extends ServiceImpl<RealmsMapper, RealmsPO>
        implements RealmsDao {
    @Resource
    private UniUserSingleAssembler uniUserSingleAssembler;

    @Override
    public void add(RealmsEntity item) {
        super.save(uniUserSingleAssembler.fromRealmsEntity(item));
    }

    @Override
    public void update(RealmsEntity item) {
        super.updateById(uniUserSingleAssembler.fromRealmsEntity(item));
    }

    @Override
    public void removeWithId(Serializable id) {
        super.removeById(id);
    }

    @Override
    public RealmsEntity getWithId(Serializable id) {
        return uniUserSingleAssembler.fromRealms(super.getById(id));
    }

    @Override
    public List<RealmsEntity> getRealmList() {
        return uniUserSingleAssembler.fromRealms(super.list());
    }

    @Override
    public List<SiRealmDTO> queryRealmList(List<String> realmIds) {
        List<RealmsPO> result = super.lambdaQuery()
                .in(RealmsPO::getId, realmIds)
                .list();
        return null == result ? null : convertToPlatform(result);
    }

    @Override
    public SiPageResult<SiRealmDTO> getRealmPage(SiRealmQueryPageIn req) {
        Page<RealmsPO> page = new Page<>(req.getCurrent(), req.getPageSize());

        page = super.lambdaQuery()
                .like(StrUtil.isNotBlank(req.getName()), RealmsPO::getRealmName, req.getName())
                .ge(StrUtil.isNotBlank(req.getStartTime()), RealmsPO::getCreateTime, req.getStartTime())
                .le(StrUtil.isNotBlank(req.getEndTime()), RealmsPO::getCreateTime, req.getEndTime())
                .orderByDesc(RealmsPO::getCreateTime)
                .page(page);
        return PageUtil.convertPage(page, this::convertToPlatform);
    }

    public List<SiRealmDTO> convertToPlatform(List<RealmsPO> plList) {
        List<SiRealmDTO> targetPl = new ArrayList<>();

        plList.forEach(item -> {
            SiRealmDTO plItem = new SiRealmDTO();
            plItem.setId(item.getId());
            plItem.setProviderId(item.getProviderId());
            plItem.setRealmCode(item.getRealmCode());
            plItem.setRealmName(item.getRealmName());
            plItem.setState(item.getState());
            plItem.setOrgCode(item.getOrgCode());
            plItem.setOrgName(item.getOrgName());
            plItem.setOrgContact(item.getOrgContact());
            plItem.setUserUrl(item.getUserUrl());
            plItem.setUpdateTime(item.getUpdateTime());
            plItem.setCreateTime(item.getCreateTime());
            targetPl.add(plItem);
        });

        return targetPl;
    }
}
